package com.sd.coupon.controller;


import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sd.coupon.entity.User;
import com.sd.coupon.service.IUserService;
import com.sd.coupon.util.rest.RestResponse;

@Controller
public class UserController {

	@Resource
	IUserService userService;

	@RequestMapping(value = "/user/login", method = RequestMethod.POST)
	@ResponseBody
	public RestResponse addUser(User user) {

		if(user==null
				|| (StringUtils.isEmpty(user.getTaobao_open_id()) && StringUtils.isEmpty(user.getTaobao_open_sid()))){
			String error ="参数异常，请确认参数正确";
			RestResponse.failure(error );
		}

		QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		queryWrapper.eq(true,"taobao_open_id", user.getTaobao_open_id()).or(true).eq(true,"taobao_open_sid", user.getTaobao_open_sid());
		User dataUser = userService.getOne(queryWrapper);

		if(dataUser!=null){
			UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();
			updateWrapper.eq(true,"taobao_open_id", user.getTaobao_open_id()).or(true).eq(true,"taobao_open_sid", user.getTaobao_open_sid());
			userService.update(user, updateWrapper);
		}else{
			userService.save(user);
		}

		return RestResponse.success();
	}


}
